﻿using DeHeng_Mes.Model.Table;
using DeHeng_Mes.Utils;
using GXWA_DATA.Model.ApiModel;
using GXWA_DATA.Model.DetailModel;
using GXWA_DATA.Model.Table;
using GXWA_DATA.Utils;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Dm.net.buffer.ByteArrayBuffer;

namespace GXWA_DATA.Dao
{
    public class DataDao
    {
        /// <summary>
        /// 查询设备配置数据
        /// </summary>
        /// <returns></returns>
        public List<DeviceConfig> QueryDeviceConfig()
        {
            return SqlSugarConfig.Db.Queryable<DeviceConfig>().ToList();
        }

        /// <summary>
        /// 查询未结束的工单
        /// </summary>
        public List<OrderStatus> QueryOrder()
        {
            return SqlSugarConfig.Db.Queryable<OrderStatus>().Where(x => x.Status == "1").ToList();
        }

        /// <summary>
        /// 存储工单数据
        /// </summary>
        /// <param name="orderData"></param>
        public void SaveOrder(OrderStatus orderData)
        {
            SqlSugarConfig.Db.Insertable(orderData).ExecuteCommand();

            Tools.ProduceLogText($"存储一条工单数据，工单号[{orderData.OrderId}]，物料编码[{orderData.MaterialName}]，物料名称[{orderData.MaterialName}]，下发数量[{orderData.ProductionPlan}]，班次[{orderData.ShiftName}]");
        }

        /// <summary>
        /// 结束工单
        /// </summary>
        /// <param name="orderData"></param>
        public void OrderClose(OrderApiCloseModel? orderData)
        {
            SqlSugarConfig.Db.Updateable<OrderStatus>().SetColumns(x => x.Status == "9").Where(x => x.OrderId == orderData.OrderCode).ExecuteCommand();

            Tools.ProduceLogText($"[{orderData.OrderCode}]结单，更新状态为[9]");
        }

        /// <summary>
        /// 存储弧焊数据
        /// </summary>
        /// <param name="traceWeld"></param>
        public void SaveWeldData(TraceWeld traceWeld)
        {
            SqlSugarConfig.Db.Insertable(traceWeld).AS($"trace_weld{HashUtils.GetHashAsInt(traceWeld.TraceId, 10)}").ExecuteCommand();
        }

        /// <summary>
        /// 存储工单与工件关联数据
        /// </summary>
        /// <param name="orderTrace"></param>
        public void SaveTraceId(OrderTrace orderTrace)
        {
            SqlSugarConfig.Db.Insertable(orderTrace).ExecuteCommand();   
        }

        /// <summary>
        /// 存储过站数据
        /// </summary>
        /// <param name="partProcess"></param>
        public void SaveStationData(PartProcess partProcess)
        {
            SqlSugarConfig.Db.Insertable(partProcess).AS($"trace_process{HashUtils.GetHashAsInt(partProcess.TraceId, 10)}").ExecuteCommand();
        }

        /// <summary>
        /// 查询过站数据
        /// </summary>
        /// <param name="traceId"></param>
        /// <param name="station"></param>
        /// <returns></returns>
        public List<PartProcess> GetStationData(string traceId, string station)
        {
            return SqlSugarConfig.Db.Queryable<PartProcess>().AS($"trace_process{HashUtils.GetHashAsInt(traceId, 10)}").Where(x => x.TraceId == traceId && x.Station == station).ToList();
        }

        /// <summary>
        /// 更新出站时间
        /// </summary>
        /// <param name="id"></param>
        /// <param name="now"></param>
        /// <param name="result"></param>
        public void UpdateEndTime(int id, DateTime now, string result, string traceId)
        {
            SqlSugarConfig
                .Db
                .Updateable<PartProcess>()
                .AS($"trace_process{HashUtils.GetHashAsInt(traceId, 10)}")
                .SetColumns(x => x.EndTime == now)
                .SetColumns(x => x.Result == result)
                .Where(x => x.Id == id)
                .ExecuteCommand();
        }

        /// <summary>
        /// 存储产量数据
        /// </summary>
        /// <param name="productions"></param>
        public void SaveProductionData(List<TraceProduction> productions)
        {
            SqlSugarConfig.Db.Insertable(productions).ExecuteCommand();
        }

        /// <summary>
        /// 产量查询
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public List<TraceProduction> QueryProduction(string orderId)
        {
            return SqlSugarConfig.Db.Queryable<TraceProduction>().Where(x => x.OrderId == orderId).ToList();
        }

        /// <summary>
        /// 工单是否存在
        /// </summary>
        /// <param name="orderCode"></param>
        /// <returns></returns>
        public bool OrderIsSaveByOrderId(string orderCode)
        {
            return SqlSugarConfig.Db.Queryable<OrderStatus>().Any(x => x.OrderId == orderCode);
        }

        /// <summary>
        /// 更新产量
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="station"></param>
        /// <param name="count"></param>
        public void UpdateProduction(string orderId, string station, int count)
        {
            SqlSugarConfig.Db.Updateable<TraceProduction>().SetColumns(x => x.ProductionNum == count).Where(x => x.OrderId == orderId && x.Station == station).ExecuteCommand();
        }

        /// <summary>
        /// 更新Ng产量
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="station"></param>
        /// <param name="count"></param>
        public void UpdateNgProduction(string orderId, string station, int count)
        {
            SqlSugarConfig.Db.Updateable<TraceProduction>().SetColumns(x => x.ProductionNum == count).Where(x => x.OrderId == orderId && x.Station == station).ExecuteCommand();
        }
    }
}
